package com.xywy.me;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.baidu.android.pushservice.PushManager;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.UserKeeper;

public class SetupPushActivity extends BaseActivity {
	private ToggleButton toggle;
	private SharedPreferences sp;
	public static final int UNPUSH = 1;
	public static final int PUSH = 0;
	public static final String PUSHSTATE = "pushstate";
	private String userid;
	private Drawable drawable;
	private int isPush;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_setup_push);

		this.preInit();
		this.initView();
		this.loadData();
	}

	@Override
	public void preInit() {
		// TODO Auto-generated method stub

	}

	@Override
	public void loadData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.me_setup_push_title_bar);
		titleBar.onClickBack(this);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		tvTitle.setText(getString(R.string.me_setup_push));
		tvBack.setText(getString(R.string.me_set));
		toggle = (ToggleButton) findViewById(R.id.toggle);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
//		userid = sp.getString("userid", "");
		userid = UserKeeper.getCurrentUser(this).getUserid();
	    isPush = sp.getInt(userid + PUSHSTATE, 0);
		drawable = getResources().getDrawable(R.drawable.me_remind_point);
		drawable.setBounds(0, 0, drawable.getMinimumWidth(),
				drawable.getMinimumHeight());
		if (isPush == PUSH) {
			toggle.setCompoundDrawables(null, null, drawable, null);
			toggle.setBackgroundResource(R.drawable.me_remind_normal);
			toggle.setChecked(true);
		} else {
			toggle.setCompoundDrawables(drawable, null, null, null);
			toggle.setBackgroundResource(R.drawable.me_remind_pressed);
			toggle.setChecked(false);
		}
		toggle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
				if(isPush ==PUSH){
					toggle.setCompoundDrawables(drawable, null, null, null);
					toggle.setBackgroundResource(R.drawable.me_remind_pressed);
					toggle.setChecked(false);
					isPush = UNPUSH; 
					sp.edit().putInt(userid + PUSHSTATE, isPush).commit();
					PushManager.stopWork(SetupPushActivity.this);
				}else {
					toggle.setCompoundDrawables(null, null, drawable, null);
					toggle.setBackgroundResource(R.drawable.me_remind_normal);
					toggle.setChecked(true);
					isPush = PUSH; 
					sp.edit().putInt(userid + PUSHSTATE, isPush).commit();
				}
			}
		});
	}

}
